package com.example.eddie.test.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.example.eddie.test.MyAdapter.NewsAdapter;
import com.example.eddie.test.PoJo.News;
import com.example.eddie.test.R;
import com.example.eddie.test.Util.Loader;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.Transformer;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

public class FirFragment extends Fragment {

    private ListView NewsListView;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view= inflater.inflate(R.layout.fragment01, container, false);

        init(view);
        initBanner(view);

        getInformation();
        return view;
    }

    private void init(View view) {
        NewsListView = view.findViewById(R.id.information);
    }

    private void getInformation() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                URL url = null;
                try {
                    url = new URL("http://59.79.112.9/_data/index_newsall.aspx");
                    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                    InputStream inputStream = connection.getInputStream();
                    BufferedReader bf = new BufferedReader(new InputStreamReader(inputStream,"GB2312"));
                    StringBuilder sb = new StringBuilder();
                    String i;
                    while ((i = bf.readLine())!=null){
                        sb.append(i);
                    }

                    String htmlString = sb.toString();
                    final List<News> list = jsoupInfo(htmlString);

                    getActivity().runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            NewsAdapter adapter = new NewsAdapter(getActivity(),R.layout.news_layout,list);
                            NewsListView.setAdapter(adapter);
                        }
                    });

                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        }).start();

    }

    //轮播图片
    private void initBanner(View view){
        List list_path = new ArrayList<>();
        List list_title = new ArrayList<>();
        list_path.add("http://www.lnfvc.edu.cn/newweb/xygk/images/教学楼1.JPG");
        list_path.add("http://www.lnfvc.edu.cn/newweb/xygk/images/文体馆1.jpg");
        list_path.add("http://www.lnfvc.edu.cn/newweb/xygk/images/校园一角1.JPG");
        list_path.add("http://www.lnfvc.edu.cn/newweb/xygk/images/业余生活1%20%20.jpg");
        list_path.add("http://www.lnfvc.edu.cn/newweb/xygk/images/中美大都会电销实训基地1.jpg");
        list_path.add("http://www.lnfvc.edu.cn/newweb/xygk/images/学生寝室1.jpg");
        list_title.add("教学楼");
        list_title.add("文体馆");
        list_title.add("校园一角");
        list_title.add("业余生活");
        list_title.add("中美大都会电销实训基地");
        list_title.add("学生寝室");
        Banner banner = (Banner) view.findViewById(R.id.banner);
        banner.setBannerStyle(BannerConfig.CIRCLE_INDICATOR_TITLE);
        banner.setImageLoader(new Loader());
        banner.setImages(list_path);
        banner.setBannerAnimation(Transformer.DepthPage);
        banner.setBannerTitles(list_title);
        banner.isAutoPlay(true);
        banner.setDelayTime(3000);
        banner.setIndicatorGravity(BannerConfig.CENTER);
        banner.start();
    }

    private List<News> jsoupInfo(String htmlString){

        List<News> list = new ArrayList<>();

        Document doc = Jsoup.parse(htmlString);
        Elements tbodyElement = doc.select("table");
        String tbody = tbodyElement.toString();
        Elements doc1 = Jsoup.parse(tbody).select("a");

        for (Element element:doc1){
            //二级菜单
            String onclick = element.attr("onclick");
            //内容 日期
            String text = element.text();
            String[] split = text.split("\\[");
            News news = new News(onclick,split[0],split[1]);

            list.add(news);
        }

        return list;

    }


}
